package com.first.xiinn.dao.impl;

import java.util.ArrayList;
import java.util.List;

import com.first.core.dao.impl.BaseDaoImpl;
import com.first.core.util.PagingBean;
import com.first.xiinn.dao.UserDao;
import com.first.xiinn.model.BookType;
import com.first.xiinn.model.User;

public class UserDaoImpl extends BaseDaoImpl<User> implements UserDao{

	public UserDaoImpl() {
		super(User.class);
	}

	public User getUser(String key) {
		String hql=" from User u where name=? or email=? or phone=?";
		Object[] params = { key,key,key };
		List users = findByHql(hql, params);
		if(!users.isEmpty()){
			return (User)users.get(0);
		}
		return null;
	}
	public List getforgotten(String name,String email) {
		String hql = "from User t where t.name = ? and t.email=?";
		Object[] params = {name,email};
		List list = this.findByHql(hql,params);
		return list;
	}
	public User getregister(User u) {
		return this.save(u);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.first.xiinn.dao.BookDao#searchAutor(java.lang.String)
	 */
	public List searchAutor(String userName) {
		String hql = "select new User(u.id as id,u.name as name) from User u ";
		if(!"".equals(userName)){
			hql+= "where u.name like '%"+userName+"%'";
		}
		List list = null ;
		try {
			list = findByHql(hql);
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
		}
		return list;
	}

	public User getById(String id) {
		String hql = "select new User(u.id as id,u.name as name) from User u where u.id = '"+id+"'";
		List list = null ;
		User user = new User();
		try {
			list = findByHql(hql);
			if(list!=null && list.size()>0){
				user = (User)list.get(0);
			}
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
		}
		return user;
	}
	
	public List<User> list() {
		String hql = " from User u where u.delFlag = '0' ";
		return findByHql(hql, null);
	}
	
	public List<User> list(PagingBean pb) {
		String hql = " from User u where u.delFlag = '0'";
		return findByHql(hql, null, pb);
	}
	
	@Override
	public List loginSearch(String login_id, String password) {
		StringBuffer hql = new StringBuffer("from User u where 1=1 ");
		Object [] obj = null;
		if(login_id!=null &&!"".equals(login_id)){
			if(login_id.indexOf("@")!=-1){
				hql.append(" and u.email ='"+login_id+"' ");
			}else{
				hql.append(" and u.phone='"+login_id+"' ");
			}
			
		}
		if(password!=null && !"".equals(password)){
			hql.append(" and u.password = '"+password+"'");
		}
		List list = this.findByHql(hql.toString());
		return list;
	}

	@Override
	public User ibGetUserByEmail(String email) {
		return (User)sqlMapClientTemplate.queryForObject("getUserByEmail", email);
	}
	
	@Override
	public User ibGetUserByVerifyData(String verifyData) {
		return (User)sqlMapClientTemplate.queryForObject("getUserByVerifyData", verifyData);
	}
	
}
